package light.head.controller.el;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.el.ClassesDAO;
import light.head.dao.el.UserClassesRelationDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.Classes;
import light.head.model.el.UserClassesRelation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 学生报名管理
 */
@Controller
@RequestMapping(Url.C_STUDENTSIGN)
public class StudentSignController extends SupporterController{
	private UserClassesRelationDAO ucrDAO;
	private ClassesDAO cDAO;
	private OrganDAO organDAO;
	

	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.SIGN_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.DELETED,Parm.TYPE,Parm.TIME1,Parm.TIME2);
		if(ucrDAO.queryStudentSignInfoByMap(organDAO.getSonCodeList(oc),cu_code(request), map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.SIGN_LIST);
	}
	
	@RequestMapping(Url.SAVE)
	public void query(Integer code ,HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.SUCCESS;
		if(code != null){
			Classes c = cDAO.get(code);
			if(c != null){
				Byte how = c.getHow();
				if(how != null){
					UserClassesRelation ucr = new UserClassesRelation();
					ucr.setClc(code);
					ucr.setUc(cu_code(request));
					ucr.setSignTime(new Date());
					if(how == 1){
						ucr.setStatus(Byte.parseByte(Sys.S_THREE));
					}else if(how == 2){
						ucr.setStatus(Byte.parseByte(Sys.S_ONE));
					}
					result = ucrDAO.add(ucr);
				}
			}
		}
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@Autowired
	public void setUcrDAO(UserClassesRelationDAO ucrDAO) {
		this.ucrDAO = ucrDAO;
	}
	@Autowired
	public void setcDAO(ClassesDAO cDAO) {
		this.cDAO = cDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
